<?php

// Include shared script

require_once('shared.php');


// Delete data in database table sherpa_importData

$db->query('DELETE FROM sherpa_importData');


// Prepare database statement to insert records from CSV file

$stmt = $db->prepare(
	'INSERT INTO	sherpa_importData
					(articleNumber,
					eanCode,
					productName,
					productLine,
					available,
					availabilityText,
					active,
					actualStock,
					obergruppe,
					untergruppe,
					size,
					priceCHF,
					oldPriceCHF,
					ready,
					created)
	VALUES			(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, NOW())'
);


// Process CSV file

$fh = fopen('../data/WebArtikel.csv','r');

$i = 0;
$fieldNames = array();

while (($data = fgetcsv($fh, null, ';')) !== false ) {

	if ($i == 0) {
		$fieldNames = $data;
	}

	else {
		$data = array_combine($fieldNames, $data);
		$stmt->execute(array(
			nullify($data['parentID']),
			nullify($data['EAN']),
			nullify($data['productName']),
			nullify($data['productLine']),
			nullify($data['availableYN']),
			nullify($data['availabilityText']),
			nullify($data['activeYN']),
			nullify($data['actualStock']),
			nullify($data['Obergruppe']),
			nullify($data['Untergruppe']),
			nullify($data['size']),
			nullify($data['priceCHF']),
			nullify($data['oldPriceCHF'])
		));
	}

	$i++;
}

fclose($fh);